package hut.game;

import hut.game.sound.Music;
import hut.game.table.VanCo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	int SCREEN_WIDTH ;
	int SCREEN_HEIGHT ;
	public static float mScale;
	public static int _unit_Width;
	public static int _unit_Height;
	Button btnOnePlayer;
	Button btnTwoPlayer;
	Button btnOption;
	Button btnExit;
	
	MyCustomDialog dialog;
	
	public static final String HELPINFOR = "Chinese chess (Xiangqi) is a very old board game in China and it has a long"
			+ " history. The earliest game mentioned by historical documents can be dated back to 4th "
			+ " BC, a game played between TianWen and Lord MengWhang, but it was probably "
			+ " invented even long before 4th BC. It is believed that Chinese chess was used to model "
			+ " wars and predict the results at that time. The early Chinese chess took very different "
			+ " forms from the modern one. It kept changing as Chinese history evolves. Significant "
			+ " changes can be identified during the Spring and Autumn Period, Tang Dynasty, Song "
			+ " Dynasty and Ming Dynasty.";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		Display display = ((WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		mScale = getBaseContext().getResources().getDisplayMetrics().density;
		Log.d("scale", ": " + mScale);
        SCREEN_WIDTH  = display.getWidth();
        SCREEN_HEIGHT = display.getHeight();
        _unit_Width = SCREEN_WIDTH/8;
        _unit_Height = SCREEN_HEIGHT/10;
		btnOnePlayer = (Button) findViewById(R.id.one_player);
		btnOnePlayer.setOnClickListener(this);
//		btnOnePlayer.setVisibility(View.GONE);
		btnTwoPlayer = (Button) findViewById(R.id.two_player);
		btnTwoPlayer.setOnClickListener(this);
		btnOption = (Button)findViewById(R.id.option);
		btnOption.setOnClickListener(this);
		btnExit = (Button) findViewById(R.id.exit);
		btnExit.setOnClickListener(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mMenuInflater = getMenuInflater();
		mMenuInflater.inflate(R.menu.options, menu);
		menu.getItem(0).setIcon(android.R.drawable.ic_menu_help);
		menu.getItem(1).setIcon(android.R.drawable.ic_menu_info_details);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.opmenu_about:
			dialog = new MyCustomDialog(this);
			dialog.KhoiTao("Author: CuongPX",0);
			dialog.show();
			break;
		case R.id.opmenu_help:
			help();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
		
	}
	@Override
	protected void onResume() {
		Music.play(this, R.raw.u, true);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Music.stop(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Music.stop(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.one_player:
			VanCo.state = false;
			Intent i = new Intent(MainActivity.this, GameActivity.class);
			startActivity(i);
			break;
		case R.id.two_player:
			VanCo.state = true;
			Intent i1 = new Intent(MainActivity.this, GameActivity.class);
			startActivity(i1);
			break;
		case R.id.option:
			Intent i2 = new Intent(MainActivity.this, Option.class);
			startActivity(i2);
			break;
		case R.id.exit:
			finish();
			break;
		default:
			break;
		}
	}
	
	private void help() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Help");
		builder.setMessage(HELPINFOR);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.show();
	}
}